namespace AbpPlus.Events
{
	using System.Collections.Generic;
	using System.Linq;
	using System.Threading;
	using System.Threading.Tasks;

	using MediatR;

	/// <summary>
	/// The domain notification handler.
	/// </summary>
	public class DomainNotificationHandler : INotificationHandler<DomainNotification>
	{
		/// <summary>
		/// The _notifications.
		/// </summary>
		private List<DomainNotification> _notifications;

		/// <summary>
		/// Initializes a new instance of the <see cref="DomainNotificationHandler"/> class.
		/// </summary>
		public DomainNotificationHandler()
		{
			this._notifications = new List<DomainNotification>();
		}

		/// <summary>
		/// The handle.
		/// </summary>
		/// <param name="message">
		/// The message.
		/// </param>
		public void Handle(DomainNotification message)
		{
			this._notifications.Add(message);
		}

		/// <summary>
		/// The get notifications.
		/// </summary>
		/// <returns>
		/// The <see>
		///         <cref>List</cref>
		///     </see>
		///     .
		/// </returns>
		public virtual List<DomainNotification> GetNotifications()
		{
			return this._notifications;
		}

		/// <summary>
		/// The has notifications.
		/// </summary>
		/// <returns>
		/// The <see cref="bool"/>.
		/// </returns>
		public virtual bool HasNotifications()
		{
			return this.GetNotifications().Any();
		}

		/// <summary>
		/// The dispose.
		/// </summary>
		public void Dispose()
		{
			this._notifications = new List<DomainNotification>();
		}

		/// <summary>
		/// The handle.
		/// </summary>
		/// <param name="notification">
		/// The notification.
		/// </param>
		/// <param name="cancellationToken">
		/// The cancellation token.
		/// </param>
		/// <returns>
		/// The <see cref="Task"/>.
		/// </returns>
		public async Task Handle(DomainNotification notification, CancellationToken cancellationToken)
		{
			this.Handle(notification);
			await Task.FromResult(0);
		}
	}
}